<?php

namespace App\Http\Controllers\Backend;


class SiteTypeController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();

        $this->model = 'SiteType';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 返回列表数据
    public function lists()
    {
        $where = $this->listWhere();
        $page  = $this->page;
        $rows  = $this->rows;
        $model = $this->checkModel($this->model);
        if($model === false){
            return $this->error($this->error);
        }

        // 模型 和 查询
        if($where){
            $result = $model->listPage($where, $page, $rows, "rank ASC");
        } else{
            $result = $model->isCache(true)->listPage($where, $page, $rows, "rank ASC");
        }

        return $this->success($result);
    }


    // 排序
    public function rank()
    {
        $_value = $this->getData('rank', 0, 'intval');
        $this->throwIf(is_null($_value), '请填写排序数字！');

        return $this->changeField('rank', $_value);
    }

    // 列表条件
    protected function listWhere()
    {
        $where   = [];
        $keyword = $this->getData('keyword', null, 'trim');
        $channel = $this->getData('channel', null, 'trim');
        if($channel){
            $where[] = ['channel', '=', $channel];
        }
        if($keyword){
            $where[] = ['name', 'like', "%$keyword%"];
        }

        return $where;
    }


    // 添加
    protected function _before_add()
    {
        $data = [
            'channel' => $this->getData('channel', null, 'trim'),
            'name'    => $this->getData('name', null, 'trim'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'channel' => $this->getData('channel', null, 'trim'),
            'name'    => $this->getData('name', null, 'trim'),
        ];

        return $data;
    }

}


